// -*- coding: utf-8 -*- 
/**
 * Project: AlgorithmsLearn
 *
 * @author: yanking
 * Create time: 2022-03-08 15:51
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package com.LeetCodeT.TREE;

import com.DataStruct.Tree.TwoBinaryTree.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

public class MaxDepth {
    /**
     * 获取给定二叉树的深度
     */
    public int maxDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        // 使用二叉树的层序遍历
        Queue<TreeNode> auxiliaryQueue = new LinkedList<>();
        auxiliaryQueue.offer(root);
        int layer = 1, nLayer = 0, ans = 0;
        while (!auxiliaryQueue.isEmpty()) {
            TreeNode p = auxiliaryQueue.poll();
            layer--;
            if (p.left != null) {
                auxiliaryQueue.offer(p.left);
                nLayer++;
            }
            if (p.right != null) {
                auxiliaryQueue.offer(p.right);
                nLayer++;
            }
            if (layer == 0) {
                layer = nLayer;
                ++ans;
                nLayer = 0;
            }
        }
        return ans;
    }




}
